package com.liveinstars.common.context;

/**
 * Created by guoyanjiang on 2018/7/9.
 */
public class BaseWebContext {

    public static ThreadLocal<Info> context = new ThreadLocal<>();

    public static void destroy() {
        context.remove();
    }

    static class Info {
        String ip;
        String from;
        String userAgent;

        public Info() {
        }

        public Info(String ip, String from, String userAgent) {
            this.ip = ip;
            this.from = from;
            this.userAgent = userAgent;
        }
    }

    public static void initInfo(String ip, String from, String userAgent) {
        context.set(new Info(ip, from, userAgent));
    }

    public static String getIP() {
        Info info = context.get();
        return info == null ? null : info.ip;
    }

    public static void setIP(String ip) {
        if (context.get() == null) {
            context.set(new Info());
        }
        context.get().ip = ip;
    }

    public static String getFrom() {
        Info info = context.get();
        return info == null ? null : info.from;
    }

    public static void setFrom(String from) {
        if (context.get() == null) {
            context.set(new Info());
        }
        context.get().from = from;
    }

    public static String getUserAgent(){
        Info info = context.get();
        return info == null ? null : info.userAgent;
    }

}
